package com.concerneyes.mine.TabActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.concerneyes.R;
import com.concerneyes.mine.Task.ExtensionImpl;
import com.concerneyes.mine.Task.ExtensionListener;
import com.concerneyes.mine.adapter.MyExtensionAdapter;
import com.concerneyes.mine.model.MyExtensionModel;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import common.AbsActivity;
import common.WebViewActivity;
import http.JsonMap;
import http.Url;
import util.ActivityUtil;

/**
 * 推广活动
 */

public class MyExtension extends AbsActivity implements AdapterView.OnItemClickListener,ExtensionListener{
    private ListView lvExtension;
    private MyExtensionAdapter adapter;
    private ExtensionImpl impl;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_extension);
        setTitle(R.string.extension_center);
        initView();
    }

    private void initView() {
        lvExtension = (ListView) findViewById(R.id.lv_extension);
        lvExtension.setOnItemClickListener(this);
        impl = new ExtensionImpl(this);
        impl.extensionImpl(Url.ExtensionList);
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int activityId = adapter.getItem(position).getId();
        Bundle bundle = new Bundle();
        bundle.putString("activityUrl",Url.ActivityDetail+String.valueOf(activityId));
        String title = getResources().getString(R.string.extension_detail);
        bundle.putString("title",title);
        ActivityUtil.setIntent(this, WebViewActivity.class,bundle);
    }

    @Override
    public void onExtensionSuccess(List<JsonMap> data) {
        String datas = data.toString();
        try {
            List<MyExtensionModel> models = new Gson().fromJson(datas, new TypeToken<ArrayList<MyExtensionModel>>() {
            }.getType());
            adapter = new MyExtensionAdapter(MyExtension.this, models);
            lvExtension.setAdapter(adapter);
        } catch (Exception e) {
        }
    }

    @Override
    public void onExtensionFail() {

    }
}
